
package com.hlkj.pay.controller.home;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern;
import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.home.*;
import com.hlkj.pay.service.home.IWorkbenchStatisticsService;
import com.hlkj.pay.util.DateUtils;
import com.hlkj.pay.vo.order.req.HomePageStatisticsReq;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author HlpayTeam
 * @date 2024/09/12 11:32
 */
@Tag(name = "首页工作台")
@RestController
@RequestMapping("/v1/workbench")
@RequiredArgsConstructor
public class WorkbenchController extends BaseController {

    private final IWorkbenchStatisticsService workbenchStatisticsService;

    @Operation(summary = "月金额统计")
    @GetMapping("/statistics/month")
    public CommonResult<Map<String, HomePageCharsStatisticsDto>> monthStatistics(HomePageStatisticsReq homePageStatisticsReq) {
        if (!StringUtils.hasText(homePageStatisticsReq.getStartTime()) || !StringUtils.hasText(homePageStatisticsReq.getEndTime())) {
            return CommonResult.error(CommonResultCode.ORDER_STATISTICS_TIME_NOT_EMPTY);
        }
        if (!DateUtils.validateDate(homePageStatisticsReq.getStartTime(), DatePattern.NORM_MONTH_PATTERN) || !DateUtils.validateDate(homePageStatisticsReq.getEndTime(), DatePattern.NORM_MONTH_PATTERN)) {
            return CommonResult.error(CommonResultCode.ORDER_STATISTICS_TIME_EXCEPTION);
        }
        HomePageCharsStatisticsQueryDto homePageCharsStatisticsQueryDto = BeanUtil.copyProperties(homePageStatisticsReq, HomePageCharsStatisticsQueryDto.class);
        homePageCharsStatisticsQueryDto.setStartQueryTime(DateUtils.shanghaiStartDate(homePageStatisticsReq.getStartTime(), DatePattern.NORM_MONTH_PATTERN));
        homePageCharsStatisticsQueryDto.setEndQueryTime(DateUtils.shanghaiEndDate(homePageStatisticsReq.getEndTime(), DatePattern.NORM_MONTH_PATTERN));
        if (DateUtils.betweenMonth(DateUtils.dateToString(homePageCharsStatisticsQueryDto.getStartQueryTime(), DatePattern.NORM_DATE_PATTERN), DateUtils.dateToString(homePageCharsStatisticsQueryDto.getEndQueryTime(), DatePattern.NORM_DATE_PATTERN)) > 6) {
            return CommonResult.error(CommonResultCode.ORDER_MONTH_STATISTICS_ERROR);
        }
        return CommonResult.success(workbenchStatisticsService.monthStatistics(homePageCharsStatisticsQueryDto));
    }

    @Operation(summary = "日金额统计")
    @GetMapping("/statistics/days")
    public CommonResult<Map<String, HomePageCharsStatisticsDto>> dayStatistics(HomePageStatisticsReq homePageStatisticsReq) {
        if (!StringUtils.hasText(homePageStatisticsReq.getStartTime()) || !StringUtils.hasText(homePageStatisticsReq.getEndTime())) {
            return CommonResult.error(CommonResultCode.ORDER_STATISTICS_TIME_NOT_EMPTY);
        }
        if (!DateUtils.validateDate(homePageStatisticsReq.getStartTime(), DatePattern.NORM_DATE_PATTERN) || !DateUtils.validateDate(homePageStatisticsReq.getEndTime(), DatePattern.NORM_DATE_PATTERN)) {
            return CommonResult.error(CommonResultCode.ORDER_STATISTICS_TIME_EXCEPTION);
        }
        if (DateUtils.betweenDay(homePageStatisticsReq.getStartTime(), homePageStatisticsReq.getEndTime()) > 6) {
            String format = String.format(CommonResultCode.ORDER_DAY_STATISTICS_ERROR.getMsg(), "7");
            return CommonResult.error(CommonResultCode.ORDER_DAY_STATISTICS_ERROR.getCode(), format);
        }
        HomePageCharsStatisticsQueryDto homePageCharsStatisticsQueryDto = BeanUtil.copyProperties(homePageStatisticsReq, HomePageCharsStatisticsQueryDto.class);
        homePageCharsStatisticsQueryDto.setStartQueryTime(DateUtils.shanghaiStartDate(homePageStatisticsReq.getStartTime(), DatePattern.NORM_DATE_PATTERN));
        homePageCharsStatisticsQueryDto.setEndQueryTime(DateUtils.shanghaiEndDate(homePageStatisticsReq.getEndTime(), DatePattern.NORM_DATE_PATTERN));
        return CommonResult.success(workbenchStatisticsService.daysStatistics(homePageCharsStatisticsQueryDto));
    }

    @Operation(summary = "当天 金额、开户数、充值统计")
    @GetMapping("/statistics/index")
    public CommonResult<HomePageStatisticsDto> index() {
        HomePageCharsStatisticsQueryDto homePageCharsStatisticsQueryDto = new HomePageCharsStatisticsQueryDto();
        String today = DateUtils.getDateString(DatePattern.NORM_DATE_PATTERN);
        HomePageStatisticsDto homePageStatisticsDto = new HomePageStatisticsDto();
        homePageCharsStatisticsQueryDto.setSnType(1);
        homePageStatisticsDto.setSumMerchants(workbenchStatisticsService.statisticsMechants(homePageCharsStatisticsQueryDto));
        homePageStatisticsDto.setSumRechargeAmount(workbenchStatisticsService.statisticsRecharge(homePageCharsStatisticsQueryDto));
        HomePageStatisticsDto orderAll = workbenchStatisticsService.statisticsOrders(homePageCharsStatisticsQueryDto);
        if (orderAll == null) orderAll = new HomePageStatisticsDto();
        homePageStatisticsDto.setSumOrderCount(orderAll.getSumOrderCount());
        homePageStatisticsDto.setSumOrderAmount(orderAll.getSumOrderAmount());
        homePageStatisticsDto.setSumFeeAmount(orderAll.getSumFeeAmount());
        homePageStatisticsDto.setSumRefundCount(orderAll.getSumRefundCount());
        homePageStatisticsDto.setSumRefundAmount(orderAll.getSumRefundAmount());
        homePageStatisticsDto.setSumChannelFeeAmount(orderAll.getSumChannelFeeAmount());

        homePageCharsStatisticsQueryDto.setStartQueryTime(DateUtils.shanghaiStartDate(today, DatePattern.NORM_DATE_PATTERN));
        homePageCharsStatisticsQueryDto.setEndQueryTime(DateUtils.shanghaiEndDate(today, DatePattern.NORM_DATE_PATTERN));
        homePageStatisticsDto.setDayMerchants(workbenchStatisticsService.statisticsMechants(homePageCharsStatisticsQueryDto));
        homePageStatisticsDto.setDayRechargeAmount(workbenchStatisticsService.statisticsRecharge(homePageCharsStatisticsQueryDto));
        HomePageStatisticsDto orderDay = workbenchStatisticsService.statisticsOrders(homePageCharsStatisticsQueryDto);
        if (orderDay == null) orderDay = new HomePageStatisticsDto();
        homePageStatisticsDto.setDayOrderCount(orderDay.getSumOrderCount());
        homePageStatisticsDto.setDayOrderAmount(orderDay.getSumOrderAmount());
        homePageStatisticsDto.setDayRefundCount(orderDay.getSumRefundCount());
        homePageStatisticsDto.setDayRefundAmount(orderDay.getSumRefundAmount());

        return CommonResult.success(homePageStatisticsDto);
    }

    @Operation(summary = "商户排名统计")
    @GetMapping("/statistics/merchant")
    public CommonResult<List<HomePageCharsStatisticsDto>> merchantStatistics(HomePageStatisticsReq homePageStatisticsReq) {
        String day = homePageStatisticsReq.getStartTime();
        if (!StringUtils.hasText(day)) {
            day = DateUtils.getDateString(DatePattern.NORM_DATE_PATTERN);
        }
        if (!DateUtils.validateDate(day, DatePattern.NORM_DATE_PATTERN)) {
            return CommonResult.error(CommonResultCode.ORDER_STATISTICS_TIME_EXCEPTION);
        }
        HomePageCharsStatisticsQueryDto homePageCharsStatisticsQueryDto = new HomePageCharsStatisticsQueryDto();
        homePageCharsStatisticsQueryDto.setStartQueryTime(DateUtils.shanghaiStartDate(day, DatePattern.NORM_DATE_PATTERN));
        homePageCharsStatisticsQueryDto.setEndQueryTime(DateUtils.shanghaiEndDate(day, DatePattern.NORM_DATE_PATTERN));
        return CommonResult.success(workbenchStatisticsService.merchantDayTop(homePageCharsStatisticsQueryDto));
    }

    @Operation(summary = "支付方式排名统计")
    @GetMapping("/statistics/payType")
    public CommonResult<List<HomePageCharsStatisticsDto>> payTypeStatistics(HomePageStatisticsReq homePageStatisticsReq) {
        String day = homePageStatisticsReq.getStartTime();
        if (!StringUtils.hasText(day)) {
            day = DateUtils.getDateString(DatePattern.NORM_DATE_PATTERN);
        }
        if (!DateUtils.validateDate(day, DatePattern.NORM_DATE_PATTERN)) {
            return CommonResult.error(CommonResultCode.ORDER_STATISTICS_TIME_EXCEPTION);
        }
        HomePageCharsStatisticsQueryDto homePageCharsStatisticsQueryDto = new HomePageCharsStatisticsQueryDto();
        homePageCharsStatisticsQueryDto.setStartQueryTime(DateUtils.shanghaiStartDate(day, DatePattern.NORM_DATE_PATTERN));
        homePageCharsStatisticsQueryDto.setEndQueryTime(DateUtils.shanghaiEndDate(day, DatePattern.NORM_DATE_PATTERN));
        return CommonResult.success(workbenchStatisticsService.payTypeStatistics(homePageCharsStatisticsQueryDto));
    }

    @Operation(summary = "日订单支付成功率统计")
    @GetMapping("/statistics/success/rate")
    public CommonResult<List<HomePageOrderSucessRateDto>> statisticsSuccessRate() {
        String today = DateUtils.getDateString(DatePattern.NORM_DATE_PATTERN);
        String startDay = DateUtils.offsetDay(today, DatePattern.NORM_DATE_PATTERN, -6);
        HomePageCharsStatisticsQueryDto homePageCharsStatisticsQueryDto = new HomePageCharsStatisticsQueryDto();
        homePageCharsStatisticsQueryDto.setStartQueryTime(DateUtils.shanghaiStartDate(startDay, DatePattern.NORM_DATE_PATTERN));
        homePageCharsStatisticsQueryDto.setEndQueryTime(DateUtils.shanghaiEndDate(today, DatePattern.NORM_DATE_PATTERN));
        return CommonResult.success(workbenchStatisticsService.statisticsSuccessRate(homePageCharsStatisticsQueryDto));
    }

    @Operation(summary = "数据库大屏主数据")
    @GetMapping("/statistics/query/index")
    public CommonResult<HomeStatisticsDto> dataQuery() {
        String today = DateUtils.getDateString(DatePattern.NORM_DATE_PATTERN);
        HomePageCharsStatisticsQueryDto homePageCharsStatisticsQueryDto = new HomePageCharsStatisticsQueryDto();
        homePageCharsStatisticsQueryDto.setStartQueryTime(DateUtils.shanghaiStartDate(today, DatePattern.NORM_DATE_PATTERN));
        homePageCharsStatisticsQueryDto.setEndQueryTime(DateUtils.shanghaiEndDate(today, DatePattern.NORM_DATE_PATTERN));
        HomeStatisticsDto homeStatisticsDto = workbenchStatisticsService.dataQuery(homePageCharsStatisticsQueryDto);

        homePageCharsStatisticsQueryDto.setSnType(1);//商户
        homeStatisticsDto.setDayMerchants(workbenchStatisticsService.statisticsMechants(homePageCharsStatisticsQueryDto));

        return CommonResult.success(homeStatisticsDto);
    }

    @Operation(summary = "对应商户日销量排行")
    @GetMapping("/statistics/merchant/top")
    public CommonResult<List<HomePageCharsStatisticsDto>> merchantDayStatistics() {
        String today = DateUtils.getDateString(DatePattern.NORM_DATE_PATTERN);
        String startDay = DateUtils.offsetDay(today, DatePattern.NORM_DATE_PATTERN, -6);

        HomePageCharsStatisticsQueryDto homePageCharsStatisticsQueryDto = new HomePageCharsStatisticsQueryDto();
        homePageCharsStatisticsQueryDto.setStartQueryTime(DateUtils.shanghaiStartDate(startDay, DatePattern.NORM_DATE_PATTERN));
        homePageCharsStatisticsQueryDto.setEndQueryTime(DateUtils.shanghaiEndDate(today, DatePattern.NORM_DATE_PATTERN));
        List<HomePageCharsStatisticsDto> list = workbenchStatisticsService.merchantDayStatistics(homePageCharsStatisticsQueryDto);
        return CommonResult.success(list);
    }
}
